<?php
class Block_Role_Admin_View_Item
    extends Block_Core_Form_Abstract
{
    protected $_formName = 'role_view';

    protected $_formId = 'admin_role_view';

    protected $_jsName = 'admin_role_view';

    public function _prepareForm()
    {
        $fieldset = new Block_Core_Form_Fieldset_Abstract();
        $fieldset->setData(array('label'=>'Role Informaton','title'=>'Role Informaton','view'=>'open'));

        $fieldset->addField(array(
            'type' => 'hidden',
            'name' => 'role[id]',
            'value'=> $this->getItemId(),
        ));

        $fieldset->addField(array(
            'title'=>'Role Name',
            'label'=>'Role Name',
            'type' =>'text',
            'value'=> $this->getName(),
            'name' =>'role[name]',
            'required'=>true,
        ));

        $resourceModel  = new Admin_Permission_Resource();
        if($this->getPermision()) {
            $value = $this->getPermision();
        } else {
            $value = '';
        }

        $value = explode(',', $value);
        if(count($value)) {
            $value = array_fill_keys($value, true);
        }
        $fieldset->addField(array(
            'title'=>'Permission',
            'label'=>'Permission',
            'type' =>'tree',
            'params'=> $resourceModel->getOptionArray(),
            'name' =>'role[permission]',
            'id'=>'role_tree',
            'required'=>true,
            'value'=>$value,
        ));

        $this->addFieldset($fieldset);
    }

    protected function _prepareData()
    {
        /* @var $controller Zend_Controller_Front */
        $controller = Zend_Controller_Front::getInstance();
        $item = $controller->getRequest()->getParam('item',null);

        if($item) {
            $model = new Model_Role_Admin_Role();
            $model->load($item);

            foreach ($model->getData() as $key=>$value) {
                $this->setData($key,$value);
            }
        }

        return parent::_prepareData();
    }
}